import os
import sys
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains
class LikeShopTest():
    def __init__(self) -> None:

        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                               'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        self.browser = webdriver.Firefox(service=service, options=options)

    def test1(self):
        self.browser.get("http://likeshop.abc:8080/admin/")
        sleep(1)
        # 定位用户名输入框输入用户名
        self.browser.find_element(By.NAME, 'account').send_keys('xiaozhi')
        # 定位密码输入框输入密码
        self.browser.find_element(By.NAME, 'password').send_keys('123456')
        # 点击登录按钮
        self.browser.find_element(By.ID, 'login').click()
        sleep(2)
        
        # 点击商品
        self.browser.find_element(By.XPATH, "/html/body/div/div[1]/div[2]/div/ul/li[1]/a").click()
        sleep(1)

        # 查找子框架
        iframe1 = self.browser.find_element(By.XPATH, "/html/body/div/div[3]/div[2]/iframe")
        # 进入子框架
        self.browser.switch_to.frame(iframe1)

        # 定位商品名称输入框
        self.browser.find_element(By.ID, "keyword").send_keys("文具")
        # 点击查询按钮
        self.browser.find_element(By.XPATH, "/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/div[3]/button[1]").click()
        sleep(2)
        
        # 定位商品列表
        tr_list = self.browser.find_elements(By.XPATH, "/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr")
        if len(tr_list) == 0:
            print("没有数据")
        else:
            is_not_bug = True
            for tr in tr_list:
                # 在tr查找td
                td = tr.find_element(By.XPATH, "./td[2]/div")
                title = td.text
                if "文具" not in title:
                    is_not_bug = False
                    print("有bug")
        if is_not_bug:
            print('没有bug')

        
        # 退出子框架
        self.browser.switch_to.default_content()
        sleep(1)
        # 定位悬停元素
        dt = self.browser.find_element(By.XPATH, "/html/body/div/div[1]/div[1]/ul[2]/li[3]/a/cite")
        # 悬停
        ActionChains(self.browser).move_to_element(dt).perform()
        sleep(1)
        # 点击退出
        self.browser.find_element(By.XPATH, "/html/body/div/div[1]/div[1]/ul[2]/li[3]/dl/dd[2]/a").click()
        sleep(1)

               
if __name__ == '__main__':
    lk = LikeShopTest()
    lk.test1()